package org.qq.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import org.qq.serverthread.ServerThread;
import org.qq.util.Util;

/**
 * 
 * project_name:QQ
 * package_name:org.qq.ui
 * file_name:ServerJframe.java
 * @author Link
 * create_date:2015年9月7日 下午3:18:14
 * modified_date:
 * description: 服务器
 *
 */
public class ServerJframe extends JFrame {
	private JTextField serverIpField;		//ip文本框
	private JTextField serverPortField;		//端口号文本框
	private JLabel serverIpLabel;			//IP标签
	private JLabel serverPortLabel;			//端口标签
	private JButton serStartButton;			//启动按钮
	private JButton serStopButton;			//停止按钮
	private JLabel timeShowLabel;			//时间显示标签
	private JLabel serStartLabel;			//服务器启动标签
	private JLabel startShowLabel;			//服务器启动时间显示标签
	private JLabel serStopLabel;			//服务器停止标签
	private JLabel stopShowLabel;			//服务器停止时间显示标签
	private JTextArea showArea;				//信息显示窗口
	private JLabel messageLabel;			//信息提示标签
	private InetAddress ia;					//IP地址
	private ServerSocket ss;				//服务器套接字
	private File file,file1,file2;			//存储用户信息文件夹,用户信息txt文档,群聊信息记录
	private ExecutorService pool = Executors.newCachedThreadPool();//线程池
	private ServerThread st;
	
	public ServerJframe() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("服务器");
		getContentPane().setLayout(null);
		getContentPane().setBackground(Color.PINK);
		this.setBounds(0, 0, 799, 620);
		Util.screen(this);
		this.setResizable(false);
		
		serverIpLabel = new JLabel("IP 地址 :");
		serverIpLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		serverIpLabel.setBounds(21, 34, 53, 31);
		getContentPane().add(serverIpLabel);
		
		try {
			ia = InetAddress.getLocalHost();//获取本地IP
		} catch (UnknownHostException e2) {
			e2.printStackTrace();
		}
		serverIpField = new JTextField();
		serverIpField.setHorizontalAlignment(SwingConstants.CENTER);
		serverIpField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		serverIpField.setBounds(85, 34, 148, 31);
		getContentPane().add(serverIpField);
		serverIpField.setColumns(10);
		serverIpField.setText(ia.getHostAddress());//获取本机IP地址作为服务器
		serverIpField.setEditable(false);
		
		serverPortLabel = new JLabel("端口号：");
		serverPortLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		serverPortLabel.setBounds(262, 34, 56, 31);
		getContentPane().add(serverPortLabel);
		
		serverPortField = new JTextField();
		serverPortField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		serverPortField.setHorizontalAlignment(SwingConstants.CENTER);
		serverPortField.setBounds(328, 34, 123, 31);
		getContentPane().add(serverPortField);
		serverPortField.setColumns(10);
		serverPortField.setText("30000");//设置默认端口号并不可编辑
		serverPortField.setEditable(false);
		
		//file = new File("D:\\mxb_codes\\QQ");//文件夹
		//file1 = new File("D:\\mxb_codes\\QQ\\用户注册信息.txt");//建用户信息文件夹以及txt文档
		//file2 = new File("D:\\mxb_codes\\QQ\\群聊信息记录.txt");
		try {
			file1 = new File(ServerJframe.class.getResource("/file/用户注册信息.txt").toURI());//建用户信息文件夹以及txt文档
			file2 = new File(ServerJframe.class.getResource("/file/群聊信息记录.txt").toURI());
		} catch (URISyntaxException e2) {
			e2.printStackTrace();
		}
		/**
		 * 启动服务器按钮
		 */
		serStartButton = new JButton("启动服务器");
		serStartButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		serStartButton.setBounds(84, 83, 138, 40);
		getContentPane().add(serStartButton);
		serStartButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				serStartButton.setEnabled(false);
				serStopButton.setEnabled(true);
				/*if(!file.exists()){//创建文件夹
					file.mkdirs();
				}*/
				if(!file1.exists()){//建存储用户信息txt文档
					try {
						file1.createNewFile();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
				if(!file2.exists()){
					try {
						file2.createNewFile();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
				
				String strip = serverIpField.getText();
				int strport = Integer.parseInt(serverPortField.getText());
				try {
					ss = new ServerSocket();
					ss.bind(new InetSocketAddress(strip, strport));//绑定服务器
					st = new ServerThread(ss);//启动服务器监听线程
					pool.submit(st);
					startShowLabel.setText(new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss").format(new Date()));
					
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
		
		serStopButton = new JButton("关闭服务器");
		serStopButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		serStopButton.setBounds(304, 83, 138, 40);
		getContentPane().add(serStopButton);
		serStopButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				pool.shutdownNow();
				System.exit(0);
			}
		});
		
		timeShowLabel = new JLabel();
		timeShowLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		timeShowLabel.setHorizontalAlignment(SwingConstants.CENTER);
		timeShowLabel.setBounds(501, 75, 250, 48);
		getContentPane().add(timeShowLabel);
		Util.timeView(timeShowLabel);//调用时间显示公共方法
		
		serStartLabel = new JLabel("服务器开启时间:");
		serStartLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		serStartLabel.setBounds(58, 143, 111, 31);
		getContentPane().add(serStartLabel);
		
		startShowLabel = new JLabel();
		startShowLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		startShowLabel.setBounds(164, 143, 171, 31);
		getContentPane().add(startShowLabel);
		
		serStopLabel = new JLabel("服务器关闭时间:");
		serStopLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		serStopLabel.setBounds(358, 143, 111, 31);
		getContentPane().add(serStopLabel);
		
		stopShowLabel = new JLabel();
		stopShowLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		stopShowLabel.setBounds(465, 143, 171, 31);
		getContentPane().add(stopShowLabel);
		
		showArea = new JTextArea();
		showArea.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		showArea.setBounds(21, 184, 751, 403);
		getContentPane().add(showArea);
		
		messageLabel = new JLabel("");
		messageLabel.setForeground(Color.RED);
		messageLabel.setFont(new Font("微软雅黑", Font.BOLD, 15));
		messageLabel.setBounds(481, 34, 279, 31);
		getContentPane().add(messageLabel);
	}
	
	public static void main(String[] args) {
		new ServerJframe().setVisible(true);
		
	}
}
